package com.fangngng.pushall.controller;

import com.fangngng.pushall.VO.IndexInitVO;
import com.fangngng.pushall.dto.Result;
import com.fangngng.pushall.entity.Tag;
import com.fangngng.pushall.entity.Topic;
import com.fangngng.pushall.service.TagService;
import com.fangngng.pushall.service.TopicService;
import com.fangngng.pushall.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author fei.fang
 * @date 2021/4/9
 **/
@Slf4j
@Controller
public class IndexController {

    @Autowired
    private TopicService topicService;

    @Autowired
    private TagService tagService;

    /**
     * 首页
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/index/init")
    @ResponseBody
    public Result<IndexInitVO> index(HttpServletRequest request) {
        log.info("首页访问, ip:{}", IpUtil.getIpAddress(request));

        // hottest 10 topic
        List<Topic> hottest10Topic = topicService.getHottest10Topic();
        List<Topic> newtest10Topic = topicService.getNewtest10Topic();
        List<Tag> hottest10Tag = tagService.getHottest10Tag();
        List<Tag> newest10Tag = tagService.getNewest10Tag();
        IndexInitVO vo = new IndexInitVO();
        vo.setHottest10Topic(hottest10Topic);
        vo.setNewtest10Topic(newtest10Topic);
        vo.setHottest10Tag(hottest10Tag);
        vo.setNewest10Tag(newest10Tag);

        // todo 最近到期的提醒 10

        return Result.ok(vo);
    }

    @GetMapping(value = {"", "/", "/index"})
    public String index(){
        return "index";
    }

}
